<template>
  <div class="dashboard-container">
    <!-- 顶部欢迎与时间区域 -->
    <el-card shadow="never" class="mt-2 dashboard-header">
      <div class="flex flex-wrap items-center justify-between">
        <!-- 左侧问候语与头像 -->
        <div class="flex items-center">
          <img
            class="w80px h80px rounded-full border border-gray-200 shadow-sm"
            :src="userStore.userInfo.avatar + '?imageView2/1/w/80/h/80'"
            alt="avatar"
          />
          <div class="ml-5">
            <p class="text-xl font-bold mb-1">{{ greetings }}</p>
            <p class="text-sm text-gray-500 mb-1">{{ weatherInfo }}</p>
            <p class="text-xs text-gray-400">{{ systemTime }}</p>
          </div>
        </div>
        <!-- 右侧快捷入口 -->
        <div class="flex space-x-4 dashboard-quick-links">
          <el-tooltip content="项目仓库" placement="bottom">
            <el-link href="https://github.com/youlaitech/vue3-element-admin" target="_blank">
              <el-icon><Folder /></el-icon>
            </el-link>
          </el-tooltip>
          <el-tooltip content="文档" placement="bottom">
            <el-link href="https://juejin.cn/post/7228990409909108793" target="_blank">
              <el-icon><Document /></el-icon>
            </el-link>
          </el-tooltip>
          <el-tooltip content="视频教程" placement="bottom">
            <el-link href="https://www.bilibili.com/video/BV1eFUuYyEFj" target="_blank">
              <el-icon><VideoCamera /></el-icon>
            </el-link>
          </el-tooltip>
        </div>
      </div>
    </el-card>

    <!-- 快捷功能区 -->
    <el-row :gutter="16" class="mt-5 dashboard-shortcuts">
      <el-col :span="6" :xs="12">
        <el-card class="shortcut-card" shadow="hover">
          <div class="flex items-center">
            <el-icon class="mr-2 text-primary"><Connection /></el-icon>
            <span>
              在线用户：
              <b>{{ onlineUserCount }}</b>
            </span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6" :xs="12">
        <el-card class="shortcut-card" shadow="hover">
          <div class="flex items-center">
            <el-icon class="mr-2 text-success"><Document /></el-icon>
            <span>
              今日访客：
              <b>{{ visitStatsData.todayUvCount }}</b>
            </span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6" :xs="12">
        <el-card class="shortcut-card" shadow="hover">
          <div class="flex items-center">
            <el-icon class="mr-2 text-warning"><VideoCamera /></el-icon>
            <span>
              今日浏览：
              <b>{{ visitStatsData.todayPvCount }}</b>
            </span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6" :xs="12">
        <el-card class="shortcut-card" shadow="hover">
          <div class="flex items-center">
            <el-icon class="mr-2 text-info"><Folder /></el-icon>
            <span>
              总用户：
              <b>{{ totalUserCount }}</b>
            </span>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 待办事项与公告 -->
    <el-row :gutter="16" class="mt-5">
      <el-col :span="8" :xs="24">
        <el-card shadow="never" class="todo-card">
          <template #header>
            <div class="flex-x-between">
              <span class="font-bold">待办事项</span>
              <el-button type="primary" size="small" @click="addTodo">添加</el-button>
            </div>
          </template>
          <el-scrollbar height="200px">
            <el-checkbox-group v-model="todoChecked">
              <el-checkbox
                v-for="(item, idx) in todoList"
                :key="item.id"
                :label="item.id"
                class="mb-2"
              >
                <span :class="{ 'line-through text-gray-400': todoChecked.includes(item.id) }">
                  {{ item.text }}
                </span>
              </el-checkbox>
            </el-checkbox-group>
          </el-scrollbar>
        </el-card>
      </el-col>
      <el-col :span="16" :xs="24">
        <el-card shadow="never">
          <template #header>
            <div class="flex-x-between">
              <span class="font-bold">最新动态</span>
              <el-link
                type="primary"
                underline="never"
                href="https://gitee.com/youlaiorg/vue3-element-admin/releases"
                target="_blank"
              >
                完整记录
                <el-icon class="link-icon"><TopRight /></el-icon>
              </el-link>
            </div>
          </template>
          <el-scrollbar height="200px">
            <el-timeline>
              <el-timeline-item
                v-for="(item, index) in vesionList"
                :key="index"
                :timestamp="item.date"
                placement="top"
                :color="index === 0 ? '#67C23A' : '#909399'"
                :hollow="index !== 0"
                size="large"
              >
                <div class="version-item" :class="{ 'latest-item': index === 0 }">
                  <div>
                    <el-text tag="strong">{{ item.title }}</el-text>
                    <el-tag v-if="item.tag" :type="index === 0 ? 'success' : 'info'" size="small">
                      {{ item.tag }}
                    </el-tag>
                  </div>
                  <el-text class="version-content">{{ item.content }}</el-text>
                  <div v-if="item.link">
                    <el-link
                      :type="index === 0 ? 'primary' : 'info'"
                      :href="item.link"
                      target="_blank"
                      underline="never"
                    >
                      详情
                      <el-icon class="link-icon"><TopRight /></el-icon>
                    </el-link>
                  </div>
                </div>
              </el-timeline-item>
            </el-timeline>
          </el-scrollbar>
        </el-card>
      </el-col>
    </el-row>

    <!-- 访问趋势统计图 -->
    <el-row :gutter="16" class="mt-5">
      <el-col :span="24">
        <el-card>
          <template #header>
            <div class="flex-x-between">
              <span>访问趋势</span>
              <el-radio-group v-model="visitTrendDateRange" size="small">
                <el-radio-button :value="7">近7天</el-radio-button>
                <el-radio-button :value="30">近30天</el-radio-button>
              </el-radio-group>
            </div>
          </template>
          <ECharts :options="visitTrendChartOptions" height="400px" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { useUserStore } from "@/store/modules/user.store";
import { useOnlineCount } from "@/composables/useOnlineCount";
import { formatGrowthRate } from "@/utils";
``;
import { useDateFormat, useNow } from "@vueuse/core";
import {
  Connection,
  Failed,
  Folder,
  Document,
  VideoCamera,
  TopRight,
} from "@element-plus/icons-vue";

// 用户信息
const userStore = useUserStore();

// 问候语
const greetings = computed(() => {
  const hours = new Date().getHours();
  const nickname = userStore.userInfo?.nickname || userStore.displayUsername;
  if (hours >= 6 && hours < 8) {
    return "晨起披衣出草堂，轩窗已自喜微凉🌅！";
  } else if (hours >= 8 && hours < 12) {
    return `上午好，${nickname}！`;
  } else if (hours >= 12 && hours < 18) {
    return `下午好，${nickname}！`;
  } else if (hours >= 18 && hours < 24) {
    return `晚上好，${nickname}！`;
  } else {
    return "偷偷向银河要了一把碎星，只等你闭上眼睛撒入你的梦中，晚安🌛！";
  }
});

// 当前系统时间
const now = useNow();
const systemTime = useDateFormat(now, "YYYY-MM-DD HH:mm:ss");

// 天气信息（可扩展为接口获取）
const weatherInfo = ref("今日天气晴朗，气温在15℃至25℃之间，东南风。");

// 在线用户
const { onlineUserCount } = useOnlineCount();

// 访客统计数据
const visitStatsData = ref({
  todayUvCount: 123,
  uvGrowthRate: 0.12,
  totalUvCount: 4567,
  todayPvCount: 234,
  pvGrowthRate: 0.08,
  totalPvCount: 7890,
});

// 总用户数（示例，可替换为接口数据）
const totalUserCount = ref(1024);

// 访问趋势图表配置
const visitTrendDateRange = ref(7);
const visitTrendChartOptions = ref({
  tooltip: { trigger: "axis" },
  legend: { data: ["浏览量(PV)", "访客数(UV)"], bottom: 0 },
  grid: { left: "1%", right: "5%", bottom: "10%", containLabel: true },
  xAxis: { type: "category", data: ["周一", "周二", "周三", "周四", "周五", "周六", "周日"] },
  yAxis: { type: "value", splitLine: { show: true, lineStyle: { type: "dashed" } } },
  series: [
    {
      name: "浏览量(PV)",
      type: "line",
      data: [120, 132, 101, 134, 90, 230, 210],
      areaStyle: { color: "rgba(64, 158, 255, 0.1)" },
      smooth: true,
      itemStyle: { color: "#4080FF" },
      lineStyle: { color: "#4080FF" },
    },
    {
      name: "访客数(UV)",
      type: "line",
      data: [220, 182, 191, 234, 290, 330, 310],
      areaStyle: { color: "rgba(103, 194, 58, 0.1)" },
      smooth: true,
      itemStyle: { color: "#67C23A" },
      lineStyle: { color: "#67C23A" },
    },
  ],
});

// 版本动态
const vesionList = ref([
  {
    id: "1",
    title: "v2.4.0",
    date: "2021-09-01 00:00:00",
    content: "实现基础框架搭建，包含权限管理、路由系统等核心功能。",
    link: "https://gitee.com/youlaiorg/vue3-element-admin/releases",
    tag: "里程碑",
  },
  {
    id: "2",
    title: "v2.3.0",
    date: "2021-08-01 00:00:00",
    content: "新增多语言支持，优化用户体验。",
    link: "https://gitee.com/youlaiorg/vue3-element-admin/releases",
    tag: "功能",
  },
  {
    id: "3",
    title: "v2.2.0",
    date: "2021-07-01 00:00:00",
    content: "修复已知 bug，提升系统稳定性。",
    link: "https://gitee.com/youlaiorg/vue3-element-admin/releases",
    tag: "修复",
  },
]);

// 待办事项
const todoList = ref([
  { id: 1, text: "完善个人信息" },
  { id: 2, text: "查看最新公告" },
  { id: 3, text: "处理待审核事项" },
  { id: 4, text: "备份数据" },
]);
const todoChecked = ref<number[]>([]);
const addTodo = () => {
  const text = prompt("请输入新的待办事项：");
  if (text) {
    todoList.value.push({ id: Date.now(), text });
  }
};
</script>

<style lang="scss" scoped>
.dashboard-container {
  position: relative;
  padding: 24px;
  background: #f7f8fa;
  min-height: 100vh;
  .dashboard-header {
    background: linear-gradient(90deg, #e0eafc 0%, #cfdef3 100%);
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(64, 128, 255, 0.05);
    .dashboard-quick-links {
      .el-link {
        font-size: 22px;
        margin-left: 8px;
      }
    }
  }
  .dashboard-shortcuts {
    .shortcut-card {
      border-radius: 10px;
      text-align: center;
      font-size: 16px;
      .el-icon {
        font-size: 22px;
      }
    }
  }
  .todo-card {
    .el-checkbox {
      width: 100%;
      margin-bottom: 8px;
    }
    .el-checkbox__label {
      font-size: 15px;
    }
  }
  .version-item {
    padding: 16px;
    margin-bottom: 12px;
    background: var(--el-fill-color-lighter);
    border-radius: 8px;
    transition: all 0.2s;
    &.latest-item {
      background: var(--el-color-primary-light-9);
      border: 1px solid var(--el-color-primary-light-5);
    }
    &:hover {
      transform: translateX(5px);
    }
    .version-content {
      margin-bottom: 12px;
      font-size: 13px;
      line-height: 1.5;
      color: var(--el-text-color-secondary);
    }
  }
}
</style>
